package org.jks.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.hibernate.exception.ConstraintViolationException;
import org.jks.config.Config;
import org.jks.model.DeviceType;
import org.jks.request.model.Ids;
import org.jks.response.model.ActionResult;
import org.jks.response.model.UITable;
import org.jks.service.DeviceTypeService;
import org.jks.util.DeviceUtil;
import org.jks.util.JsonUtil;
import org.jks.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/device_type")
public class DeviceTypeController {
	@Autowired
	DeviceTypeService deviceTypeService;

	@RequestMapping("/add")
	public void add(DeviceType deviceType, HttpServletRequest request,
			HttpServletResponse response, HttpSession session) {
		ActionResult ar = new ActionResult();
		if(!StringUtils.isEmpty(deviceType.getName())){
			try {
				deviceTypeService.save(deviceType);
				ar.setData(deviceType);
				ar.setSuccess(true);
			} catch( ConstraintViolationException e){
				ar.setMsg("设备名称冲突");
			} catch (Exception e) {
				ar.setMsg("添加设备失败");
				e.printStackTrace();
			}
		}
		else{
			ar.setMsg("设备类型名称不能为空");
		}
		
		ResponseUtil.returnJson(response, ar);
	}
	
	@RequestMapping("/delete")
	public void delete(Ids ids, HttpServletRequest request, 
			HttpServletResponse response, HttpSession session){
		ActionResult ar = new ActionResult();
		if(DeviceUtil.canDelete(ids)){
			try{
				deviceTypeService.delete(ids.getIds());
				ar.setSuccess(true);
			}
			catch(Exception e){
				ar.setMsg("删除设备类型失败");
			}
		}
		else{
			ar.setMsg("系统默认设备类型不能删除");
		}
		ResponseUtil.returnJson(response, ar);
	}
	
	@RequestMapping("/delete/{id}")
	public void delete(@PathVariable long id, HttpServletRequest request, HttpServletResponse response){
		ActionResult ar = new ActionResult();
		if(DeviceUtil.canDelete(id)){
			try{
				deviceTypeService.delete(id);
				ar.setSuccess(true);
			}
			catch(Exception e){
				ar.setMsg("删除设备类型失败");
			}
		}
		else{
			ar.setMsg("系统默认设备类型不能删除");
		}
		ResponseUtil.returnJson(response, ar);
	}
	
	@RequestMapping("/detail/{id}")
	public ModelAndView detail(@PathVariable long id, HttpServletRequest request, HttpSession session, HttpServletResponse response, Map<String,Object> model){
		try {
			DeviceType deviceType = deviceTypeService.findById(id);
			model.put("deviceType", JsonUtil.Object2Json(deviceType));
		} catch (Exception e) {
			e.printStackTrace();
		}
		Config.init(request, response, session, model);
		return new ModelAndView("wuziMgr/wzlx_detail", model);
	}
	
	@RequestMapping("/update")
	public void update(DeviceType deviceType, HttpServletRequest request, HttpServletResponse response){
		ActionResult ar = new ActionResult();
		try{
			deviceTypeService.update(deviceType);
			ar.setSuccess(true);
		}
		catch(Exception e){
			ar.setMsg(e.getMessage());
			e.printStackTrace();
		}
		ResponseUtil.returnJson(response, ar);
	}
	
	@RequestMapping("/all")
	public void all(HttpServletRequest request, HttpServletResponse response){
		ActionResult ar = new ActionResult();
		String[] limit = request.getParameterValues("limit");
		String[] pages = request.getParameterValues("page");
		int pagesize = 25;
		int page = 1;
		if(limit.length > 0){
			pagesize = Integer.valueOf(limit[0]);
		}
		if(pages.length > 0){
			page = Integer.valueOf(pages[0]);
		}
		try{
			long total = deviceTypeService.count();
			List<DeviceType> deviceTypes = deviceTypeService.list(page, pagesize);
			ar.setSuccess(true);
			UITable table = new UITable();
			table.setTotal(total);
			table.setRows(deviceTypes);
			ar.setData(table);
		}
		catch(Exception e){
			ar.setMsg(e.getMessage());
			e.printStackTrace();
		}
		ResponseUtil.returnJson(response, ar);
	}
	
	@RequestMapping("/list_no_flight")
	public void listNoFlight(HttpServletRequest request, HttpServletResponse response){
		ActionResult ar = new ActionResult();
		try{
			List<DeviceType> deviceTypes = deviceTypeService.listForSpareParts();
			ar.setSuccess(true);
			UITable table = new UITable();
			table.setRows(deviceTypes);
			ar.setData(table);
		}
		catch(Exception e){
			ar.setMsg(e.getMessage());
			e.printStackTrace();
		}
		ResponseUtil.returnJson(response, ar);
	}
}
